//
//  YGHomeSegmentView.m
//  luGongPro
//
//  Created by 王庆学 on 2017/8/25.
//  Copyright © 2017年 王庆学. All rights reserved.
//

#import "YGHomeSegmentView.h"
#import "YSUtil.h"

@interface YGHomeSegmentView ()
{
    
}
@property (weak, nonatomic) IBOutlet UILabel *mInfoLabel;
@property (weak, nonatomic) IBOutlet UILabel *mColumnLabel;

@property (strong, nonatomic) RACSubject *subject;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *MoveViewLeading;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *MoveViewWidth;

@property (nonatomic, strong) NSArray *mLabelsArray;

@end

@implementation YGHomeSegmentView

- (void)awakeFromNib{
    [super awakeFromNib];
    _currentSelect = 0;
    _mLabelsArray = @[_mInfoLabel,_mColumnLabel];
    for (UILabel *label in _mLabelsArray){
        [label setFont:kFONT([YSUtil getLenght:16])];
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:kSEL(selectAction:)];
        [singleTap setNumberOfTapsRequired:1];
        [singleTap setNumberOfTouchesRequired:1];
        [label addGestureRecognizer:singleTap];
    }
    [_MoveViewWidth setConstant:[YSUtil getLenght:70]];
}

- (void)selectAction:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == _currentSelect) return;
    [self changeLabelColor:tap.view.tag];
    [self moveLinePoint:(kWidth / 2) * tap.view.tag];
}

- (void)changeLabelColor:(NSInteger)tag{
    UILabel *mCurrentLabel = _mLabelsArray[_currentSelect];
    [mCurrentLabel setTextColor:[UIColor colorFromHexString:@"5b6879"]];
    _currentSelect = tag;
    mCurrentLabel = _mLabelsArray[_currentSelect];
    [mCurrentLabel setTextColor:[UIColor colorFromHexString:@"183e97"]];
}

- (void)moveLinePoint:(CGFloat )pointX{
    CGFloat offset = 0;
    switch (_currentSelect) {
        case 0:
        offset = 62;
        break;
        
        case 1:
        offset = 60;
        break;
        
        case 2:
        offset = 10;
        break;
        
        case 3:
        offset = 12;
        break;
    }
    [self layoutIfNeeded];
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        _MoveViewLeading.constant = pointX + [YSUtil getLenght:offset];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [_subject sendNext:@(_currentSelect)];
    }];
}

- (RACSignal *)segmentDidChange{
    if (!_subject){
        _subject = [[RACSubject alloc]init];
    }
    return _subject;
}

+ (id)loadFromXib{
    YGHomeSegmentView *segmentView = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self  class]) owner:self options:nil][0];
    return segmentView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
